package com.jackchong.utils;

import android.animation.ValueAnimator;
import android.graphics.Color;
import android.view.TextureView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.annotation.FloatRange;

import com.utils.R;

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/12/25 17:53
 */

public class AnimUtils {


    public static final int VISIBILITY_DURATION = 2000;
    public static final int VISIBILITY_DELAY = 1000;


    /**
     * time    : 2019/4/17 18:30
     * desc    : 缩小到隐藏
     * versions: 1.0
     */
    public static void scaleToHide(View view){
        if (view.getVisibility() == View.GONE){
            return;
        }
        Animation animation = AnimationUtils.loadAnimation(view.getContext(), R.anim.common_popwin_hide_anim);
        view.startAnimation(animation);
        animation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }
        });
    }


    /**
     * time    : 2019/4/17 18:30
     * desc    : 放大到显示
     * versions: 1.0
     */
    public static void scaleToShow(View view){
        if (view.getVisibility() == View.VISIBLE){
            return;
        }
        view.setVisibility(View.VISIBLE);

        Animation animation = AnimationUtils.loadAnimation(view.getContext(), R.anim.common_popwin_show_anim);
        view.startAnimation(animation);
    }


    public static void rotation(View view, float fromDegrees, float toDegrees, int repeatCount){
        RotateAnimation animation = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setRepeatCount(repeatCount);
        animation.setDuration(500);
        view.startAnimation(animation);
    }


    /**
     * time    : 2019/5/5 19:53
     * desc    : 平移+透明度渐变动画
     * versions: 1.0
     */
    public static void translationAndAlpha(View view, float fromX, float toX, float fromY, float toY, float fromAlpha, float toAlpha, long duration){
        view.setVisibility(View.VISIBLE);
        AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
        TranslateAnimation translate = new TranslateAnimation(fromX, toX, fromY, toY);

        AnimationSet set = new AnimationSet(true);
        set.addAnimation(alpha);
        set.addAnimation(translate);
        set.setDuration(duration);
        view.startAnimation(set);
    }


    /**
     * 显示, 然后慢慢变淡后隐藏
     * @param view
     */
    public static void openFadeOutAnim(View view){
        openFadeOutAnim(view, VISIBILITY_DELAY, VISIBILITY_DURATION);
    }


    /**
     * 显示, 然后慢慢变淡后隐藏
     * @param view
     */
    public static void openFadeOutAnim(View view, long delay, long duration){
        view.setVisibility(View.VISIBLE);
        AlphaAnimation animation = new AlphaAnimation(1, 0);
        animation.setDuration(duration);
        animation.setStartOffset(delay);
        animation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.INVISIBLE);
            }
        });
        view.startAnimation(animation);
    }


    /**
     * 渐变背景动画
     * @param view
     * @param startColor
     * @param endColor
     * @param duration
     */
    public static void gradientAnim(View view, int startColor, int endColor, long duration){
        view.setVisibility(View.VISIBLE);
        ValueAnimator animation = ValueAnimator.ofFloat(0, 1);
        animation.addUpdateListener(animator -> {
            float value = animator.getAnimatedFraction();
            view.setBackgroundColor(getGradientColor(startColor, endColor, value));
        });
        animation.start();
    }

    public static int getGradientColor(int startColor, int endColor, @FloatRange(from = 0, to = 1) float progress){
        int a = (byte) (progress * (Color.alpha(endColor) - Color.alpha(startColor)) + Color.alpha(startColor));
        int r = (byte) (progress * (Color.red(endColor) - Color.red(startColor)) + Color.red(startColor));
        int g = (byte) (progress * (Color.green(endColor) - Color.green(startColor)) + Color.green(startColor));
        int b = (byte) (progress * (Color.blue(endColor) - Color.blue(startColor)) + Color.blue(startColor));

        a &= 0x000000ff;
        r &= 0x000000ff;
        g &= 0x000000ff;
        b &= 0x000000ff;
        return Color.argb(a, r, g, b);
    }

    public static void numberChangeAnim(int startNum, int endNum, TextView textView) {
        ValueAnimator animator = ValueAnimator.ofInt(startNum, endNum);
        animator.setDuration(1000);
        animator.addUpdateListener(animation -> {
            int num = (int) animation.getAnimatedValue();
            textView.setText(String.valueOf(num));
        });
        animator.setStartDelay(300);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.start();
    }


    public static class AnimationListener implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}
